<?php

class IntegrationLogic extends CommonModel
{
	const USER_INTE_LIST_KEY = 'USER_INTEGRATION_LIST_';
	
	const USER_INTE_INFO_KEY = 'USER_INTEGRATION_INFO_';
	
	// 积分明细分表数目
	private $_Inte_detail_tables = 5;
	// 模型类
	private $_Model = '';
	// 缓存模型类
	private $_CacheModel = '';
	
	public function __construct() {
		$this -> _CacheModel = D('Cache', 'Service');
		$this -> _CacheModel -> init();
		$this -> _Model = new Model();
	}
	
	/**
	 * 获取用户的积分明细列表
	 */
	public function getUserIntegrationList($uId) {
		$key = self::USER_INTE_LIST_KEY . $uId;
		$list = $this -> _CacheModel -> get($key);
		$tbSuffix = $uId % $this -> _Inte_detail_tables;
	
		if (!$list) {
			$sql = 'customer_id='.$uId;
			$list = $this -> _Model -> table('customer_integration_detail_'.$tbSuffix)
					-> where($sql)
					-> select();
				
			$this -> _CacheModel -> set($key, $list);
			$this -> _CacheModel -> saveKeys($key, 'user', $uId);
		}
	
		return $list;
	}
	
	/**
	 * 添加积分记录
	 * @param string $amount
	 * @param string $type 1增加积分，2扣除积分
	 * @param string $intro 备注信息
	 */
	public function addIntegrationDetail($uId, $amount, $type, $intro='' ) {
		$key = self::USER_INTE_LIST_KEY . $uId;
		$tbSuffix = $uId % $this -> _Inte_detail_tables;
		
		$data = array(
				'customer_id' => $uId,
				'customer_name' => $this -> getCustomer(),
				'amount' => $amount,
				'type' => $type,
				'info' => $intro,
				'create_time' => time()
		);
	
		$ret = $this -> _Model -> table('customer_integration_detail_'.$tbSuffix) 
			   -> add($data);
	
		if ($ret)
			$this -> _CacheModel -> rm($key);
	
		return $ret;
	}
	
	/**
	 * 获取用户积分信息
	 * @param $uId integer
	 */
	public function getUserIntegration($uId) {
		$key = self::USER_INTE_INFO_KEY . $uId;
		
		$inteInfo = $this -> _CacheModel -> get($key);
		if (!$inteInfo) {
			$inteInfo = $this -> _Model -> table('customer_integration')
						-> where('customer_id='.$uId.' AND status=1')
						-> find();
		
			if(!$inteInfo) {
				$userBase = D('User', 'Logic') -> getUserBaseInfo($uId);
				$account = $userBase['account'];
				$this -> initIntegration($uId, $account);
			}
				
			$this -> _CacheModel -> set($key, $inteInfo);
			$this -> _CacheModel -> saveKeys($key, 'user', $uId);
		}
	
		return $inteInfo;
	}
	
	/**
	 * 更新用户积分
	 * @param $uId integer 用户ID
	 * @param $count 积分值
	 * @param $type 类型 1：增加，2：减少
	 */
	public function updateIntegration($uId, $amount, $type) {
		$key = self::USER_INTE_INFO_KEY . $uId;
	
		$data = array('update_time'=>time());
		if ($type == 1)
			$data['amount'] = array('exp', 'amount + '.intval($amount));
		else
			$data['amount'] = array('exp', 'amount - '.intval($amount));
	
		$ret = $this -> _Model -> table('customer_integration')
			   -> where('customer_id='.$uId.' AND status=1')
			   -> save($data);
	
		if ($ret)
			$this -> _CacheModel -> rm($key);
			
		return $ret;
	}
	
	/**
	 * 初始化积分信息
	 */
	public function initIntegration($uId, $account) {
		$data = array(
			'customer_id' 	=> $uId,
			'customer_name' => $account,
			'amount' 		=> 0,
			'create_time'	=> time(),
			'update_time'	=> 0,
			'status'		=> 1							
		);
		
		$ret = $this -> _Model -> table('customer_integration') -> add($data);
		
		if ($ret)
			return $data;
		
		return 0;
	}
	
	/**
	 * 判断用户积分是否充足
	 * @param $uId integer 用户ID
	 * @param $account integer 积分数目
	 * @return -1 参数错误，-2积分记录不存在 ， 1积分充足，0 积分不足
	 */
	public function checkInteEnough( $uId, $amount ) {
		if (!$amount)
			return -1;
	
		$inteInfo = $this -> getUserIntegration($uId);
		if (!$inteInfo)
			return -2;
	
		if ($inteInfo['amount'] < $amount)
			return 0;
	
		return 1;
	}
	
}